package com.sky.service.impl;

import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.OrderService;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime beginTime, LocalDateTime endTime) {
        // 1. 获取今日开始时间和结束时间
        //LocalDateTime beginTime = LocalDate.now().atTime(LocalTime.MIN);
        //LocalDateTime endTime = LocalDate.now().atTime(LocalTime.MAX);

        // 2. 获取营业额数据
        Double todayTurnover = orderMapper.getTodayTurnover(beginTime, endTime);
        if (todayTurnover == null) {
            todayTurnover = 0.0;
        }

        // 3. 获取有效订单数
        Integer todayValidOrderCount = orderMapper.countStatusAndTimeRange(5, beginTime, endTime);

        // 4. 获取订单完成率
        Double todayOrderCompletionRate = 0.0;
        if (todayValidOrderCount > 0) {
            todayOrderCompletionRate = orderMapper.countStatusAndTimeRange(6, beginTime, endTime) * 1.0 / todayValidOrderCount;
        }

        // 5. 获取平均客单价
        Double todayUnitPrice = 0.0;
        if (todayTurnover > 0) {
            todayUnitPrice = todayTurnover / todayValidOrderCount;
        }

        // 6. 新增用户数
        Map userMap = new HashMap<>();
        userMap.put("beginTime", beginTime);
        userMap.put("endTime", endTime);
        Integer todayNewUsers = userMapper.countByMap(userMap);

        // 7. 封装Vo
        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .turnover(todayTurnover)
                .validOrderCount(todayValidOrderCount)
                .orderCompletionRate(todayOrderCompletionRate)
                .unitPrice(todayUnitPrice)
                .newUsers(todayNewUsers)
                .build();
        return businessDataVO;
    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        // 1. 获取待接单数量
        Integer waitingOrders = orderMapper.countStatus(Orders.TO_BE_CONFIRMED);

        // 2. 获取待派送数量
        Integer deliveredOrders = orderMapper.countStatus(Orders.CONFIRMED);

        // 3. 获取已完成数量
        Integer completedOrders = orderMapper.countStatus(Orders.COMPLETED);

        // 4. 获取已取消数量
        Integer cancelledOrders = orderMapper.countStatus(Orders.CANCELLED);

        // 5. 获取全部订单
        Integer allOrders = orderMapper.countStatus(null);

        // 6. 封装Vo
        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
        return orderOverViewVO;
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        // 1. 获取已启售数量
        Integer sold = dishMapper.countStaus(1L);

        // 2. 获取已停售数量
        Integer discontinued = dishMapper.countStaus(0L);

        // 3. 封装Vo
        DishOverViewVO dishOverViewVO = DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return dishOverViewVO;
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        // 1. 获取已启售数量
        Integer sold = setmealMapper.countStaus(1L);

        // 2. 获取已停售数量
        Integer discontinued = setmealMapper.countStaus(0L);

        // 3. 封装Vo
        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return setmealOverViewVO;
    }
}
